package com.lxb.myforum.repository

import com.lxb.myforum.pojo.User

import org.springframework.data.jpa.repository.JpaSpecificationExecutor
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.PagingAndSortingRepository
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

/**
 *
 * @author lxb
 */
@Repository
interface UserRepository : PagingAndSortingRepository<User, Long>, JpaSpecificationExecutor<User> {
    /**
     * 根据用户名获取User信息
     * @param username 用户的用户名
     * @return
     */
    fun findUserByUsername(username: String): User

    // 删除 user与role之间的联系
    @Modifying(clearAutomatically = true)
    @Query(value = "delete from t_user_role where user_id = ?1", nativeQuery = true)
    fun delUserRole(userId: Long): Int

    @Modifying
    @Transactional
    @Query(value = "delete from t_user_role where user_id = ?1 and role_id in (?2)", nativeQuery = true)
    fun deleteRolesByUserId(userId: Int, roleIds: List<Int>)
}
